﻿package Classes.data
{
	import flash.display.Sprite;
	import flash.events.*; 
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLVariables;
	import flash.net.URLRequestMethod;
	import flash.external.*;
	
	import com.adobe.crypto.*;
	 /*
	 *rock
	 *
	 */	
	public class DataProcess extends Sprite
	{ 
		private var _userId:int;     //用户ID
		private var _gameId:int;     //游戏名字  
		private var _hostUrl:String; //服务器地址
		private var _postUrl:String;  //服务器接收数据接口地址
		
		private var _uUrlLoad:URLLoader;
		private var _sGameXmlUrl:String = "cfg\/gameXml.xml";
		
		public function DataProcess() {
		}
		
		public function cfgInit():void
		{	
		   	//userId = stage.loaderInfo.parameters["userId"];     //获取当前用户ID
			//gameId = stage.loaderInfo.parameters["gameId"]; //获取游戏名字 
			trace("Html send data:  "+"   userId="+userId+"   gameId="+gameId)
			//getXmlData();
		}
		
		//提交分数
		public function submitData(posturl:String,playscore:Number,passid:String="",accountname:String="",gameid:String="",gamemode:String="",begintime:String="",rivalpassid:String="",rivalaccountname:String="",rivallevel:String="",rivalscore:Number=0):void{
			trace("submit score " + rivalscore);
			var keyStr:String = "";			
			var loader:URLLoader=new URLLoader();
			var re:URLRequest = new URLRequest();
			var vr:URLVariables = new URLVariables();
			
			vr.playscore = playscore;
			vr.passid = passid;
			vr.accountname = accountname;			
			vr.gameid = gameid;
			vr.gamemode = gamemode;
			vr.begintime = begintime;
			vr.rivalpassid = rivalpassid;
			vr.rivalaccountname = rivalaccountname;
			vr.rivallevel = rivallevel;
			vr.rivalscore = rivalscore;
			
			keyStr = passid + accountname + gameid + playscore + "9999@mttang.com";
			vr.key = MD5.hash(keyStr);
			trace(vr.key);
			re.method = URLRequestMethod.POST;				
			re.url = posturl;
			re.data = vr;
			loader.addEventListener(Event.COMPLETE,postHandler);
			loader.addEventListener(IOErrorEvent.IO_ERROR,errorHandler);
			loader.load(re);
		}
		
		private function errorHandler(e:IOErrorEvent):void{
			trace("error");
		}
		
		private function postHandler(e:Event):void {
			trace("post complete");
			//js function transSucc
			trace(e.target.data);
			ExternalInterface.call("transSucc",e.target.data);
			dispatchEvent(new Event("postCompleteEvent"));
		}

		/**
		 * 得到XML配置的数据
		 */
		private function getXmlData():void
		{
			_uUrlLoad = new URLLoader(new URLRequest(_sGameXmlUrl));
			_uUrlLoad.addEventListener(Event.COMPLETE, urlComHandler);
			_uUrlLoad.addEventListener(IOErrorEvent.IO_ERROR, urlIOErrorHandler);
			_uUrlLoad.addEventListener(SecurityErrorEvent.SECURITY_ERROR, urlSErrorHandler);
		}
		private function urlComHandler(e:Event):void
		{
			var xml:XML = XML(_uUrlLoad.data); 
			hostUrl = xml.hosturl;
			postUrl = xml.posturl; 
			trace("所获取的参数", hostUrl, postUrl);
			dispatchEvent(new Event(Event.COMPLETE)); //抛出事件
		}
		
		private function urlIOErrorHandler(e:IOErrorEvent):void
		{
			trace("Load IOError....");
		}
		private function urlSErrorHandler(e:SecurityErrorEvent):void
		{
			trace("Load SECURITY_ERROR....");
		}
		
		//**************************************************************************
		//公用的getter和setter方法
		public function get userId():int {
			return _userId;
		}
		
		public function set userId(_val:int):void {
			_userId = _val;
		}
		
		public function get gameId():int {
			return _gameId;
		}
		
		public function set gameId(_val:int):void {
			_gameId = _val;
		}
		
		public function get hostUrl():String {
			return _hostUrl;
		}
		
		public function set hostUrl(_val:String):void {
			_hostUrl = _val;
		}
		
		public function get postUrl():String {
			return _postUrl;
		}
		
		public function set postUrl(_val:String):void {
			_postUrl = _val;
		}
		
//*************************************************************************
	}
}